package com.estate.charge.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.estate.bean.ChargeItem;
import com.estate.charge.feign.QuartersFeign;
import com.estate.charge.service.ChargeItemService;
import com.estate.mapper.ChargeItemMapper;
import com.estate.util.JsonResult;
import com.estate.util.ResultTool;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class ChargeItemServiceImpl extends ServiceImpl<ChargeItemMapper, ChargeItem> implements ChargeItemService {

    @Resource
    private QuartersFeign quartersFeign;
    @Override
    public JsonResult listByPage(int page, int size) {
        PageHelper.startPage(page, size);
        QueryWrapper<ChargeItem> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("create_time");
        List<ChargeItem> list = list(wrapper);
        list.forEach(e->{
            e.setQuarters(quartersFeign.find(e.getCommunityId()));
        });
        PageInfo<ChargeItem> pageInfo = new PageInfo<>(list);
        return new JsonResult<>(pageInfo);
    }

    @Override
    public JsonResult saveChargeItem(ChargeItem chargeItem) {
        chargeItem.setIsDelete(0);
        chargeItem.setUseType(0);
        log.info("新增chargeItem数据:{}", chargeItem);
        boolean b = save(chargeItem);
        if (!b) {
            return ResultTool.fail(501, "新增车位使用失败");
        }
        return ResultTool.success("success");
    }

    @Override
    public JsonResult updateChargeItem(ChargeItem chargeItem) {
        log.info("修改chargeItem数据:{}", chargeItem);
        boolean b = updateById(chargeItem);
        if (!b) {
            return ResultTool.fail(503, "修改chargeItem失败");
        }
        return ResultTool.success("success");
    }

    @Override
    public JsonResult deleteChargeItem(String chargeItemId) {
        log.info("删除chargeItem数据:{}", chargeItemId);
        boolean b = removeById(chargeItemId);
        if (!b) {
            return ResultTool.fail(502, "删除chargeItem失败");
        }
        return ResultTool.success("sucess");
    }
}
